#include "window_common.h"
#include "gkt_voice.h"
#include "gkt_pr_sensor.h"

#if	defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE) && (GKT_CONFIG_PROXIMITY_SENSOR_ENABLE)

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_SELECT,
	WIDGET_ID_CANCEL_OK,
	WIDGET_ID_MAX
};
static const gkt_widget_select_attr_u sc_window_pr_sensor_widget_select_attr = {
	GKT_WIDGET_SELECT_NUMBER_ATTR(GKT_PR_SENSOR_SENSE_SHUTDOWN,
		GKT_PR_SENSOR_SENSE_GEAR_MAX-1, 1,
		GKT_PR_SENSOR_SENSE_GEAR_DEFAULT,
		WINDOW_TEXT_ID_gear),
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(pr_sensor) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_proximity_detection
	),
	[WIDGET_ID_SELECT] = GKT_WIDGET_SELECT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_LARGE_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT,
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		&sc_window_pr_sensor_widget_select_attr
	),
	[WIDGET_ID_CANCEL_OK] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * 3,
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok
	),
};

typedef struct {
	uint32_t	pr_sensor_state;
} window_pr_sensor_s;
static window_pr_sensor_s s_window_pr_sensor;

static void window_pr_sensor_complete(uint32_t pr_sensor)
{
	//if (s_window_pr_sensor.pr_sensor_state != pr_sensor) {
		s_window_pr_sensor.pr_sensor_state = pr_sensor;
		gp_window_data->pr_sensor_state = pr_sensor;
		//gkt_pr_sensor_set_threshold(gp_window_data->pr_sensor_state);
		window_pr_sensor_handle();
		window_data_save();
	//}

	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
			WINDOW_TEXT_ID_proximity_detection,
			WINDOW_TEXT_ID_set_success, WINDOW_VOICE_COMBINED_ID(set_success));

	gkt_window_exit();
}

static int window_pr_sensor_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	uint32_t pr_sensor;
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				s_window_pr_sensor.pr_sensor_state = gp_window_data->pr_sensor_state;
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
				if (data) {
					gkt_widget_select_set_value(WIDGET_ID_SELECT,
						s_window_pr_sensor.pr_sensor_state);
				}
				window_common_mask_except_updn_cancel_ok();
			}
			break;
		case GKT_EVENT_WIDGET:
			if (GKT_WIDGET_EVENT_CLICK == minor_type) {
				pr_sensor = gkt_widget_select_get_value(WIDGET_ID_SELECT);
				window_pr_sensor_complete(pr_sensor);
			}
			retval = GKT_EVENT_HANDLE_COMPLETE;
			break;
		default:
			break;
	}
	
	return retval;
}
GKT_WINDOW_DESC_DEFINE(pr_sensor);

#endif

